package fun.boomcat.luckyhe.mirai.plugin.botcontrol.listener;

import fun.boomcat.luckyhe.mirai.plugin.botcontrol.pojo.Config;
import fun.boomcat.luckyhe.mirai.plugin.botcontrol.util.*;
import kotlin.coroutines.CoroutineContext;
import net.mamoe.mirai.console.command.CommandManager;
import net.mamoe.mirai.contact.*;
import net.mamoe.mirai.event.EventHandler;
import net.mamoe.mirai.event.SimpleListenerHost;
import net.mamoe.mirai.event.events.*;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.MessageChainBuilder;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * @author Lucky_He
 * @date 2021年8月23日 14:32
 * @description EventListener
 */
public class EventListener extends SimpleListenerHost {
    @Override
    public void handleException(@NotNull CoroutineContext context, @NotNull Throwable exception) {
        super.handleException(context, exception);
    }

    @EventHandler
    public void onBotOnlineEvent(BotOnlineEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("botOnlineEvent")) {
            return;
        }

        String res = "事件：" + event.getClass().getSimpleName() + "\n" +
                "已登录QQ：" + event.getBot().getId();
        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(res);
    }

    @EventHandler
    public void onBotReloginEvent(BotReloginEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("botReloginEvent")) {
            return;
        }

        String res = "事件：" + event.getClass().getSimpleName() + "\n" +
                "已重新登录QQ：" + event.getBot().getId();
        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(res);
    }

    @EventHandler
    public void onBotAvatarChangedEvent(BotAvatarChangedEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("botAvatarChangedEvent")) {
            return;
        }

        String res = "事件：" + event.getClass().getSimpleName() + "\n" +
                "头像已改变";
        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(res);
    }

    @EventHandler
    public void onBotNickChangedEvent(BotNickChangedEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("botNickChangedEvent")) {
            return;
        }

        String res = "事件：" + event.getClass().getSimpleName() + "\n" +
                "修改前：" + event.getFrom() + "\n" +
                "修改后：" + event.getTo();
        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(res);
    }

    @EventHandler
    public void onGroupMessageEvent(GroupMessageEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();

//        直接对话
        if (CurrentChattingUtil.getType().equals(CurrentChattingType.GROUP)) {
            if (event.getGroup().getId() == CurrentChattingUtil.getGroupId()) {
                BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(
                        new MessageChainBuilder().append(event.getSender().getNameCard()).append("(")
                                .append(String.valueOf(event.getSender().getId())).append(")：\n").append(event.getMessage()).build()
                );
                return;
            }
        }

        if (!config.getEvents().get("groupMessageEvent")) {
            return;
        }

        if (!config.getGroupsForGroupMessageEvent().contains(event.getGroup().getId())) {
//            不在消息群列表中时则不发送该群的消息
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("群号：").append(String.valueOf(event.getGroup().getId())).append("\n")
                .append("群名：").append(event.getGroup().getName()).append("\n")
                .append("发送者QQ：").append(String.valueOf(event.getSender().getId())).append("\n")
                .append("发送者群昵称：").append(event.getSender().getNameCard()).append("\n")
                .append("时间：").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date((long) event.getTime() * 1000L)))
                .append("\n");

        try {
            mcb.append("内容：\n").append(event.getMessage());
        } catch (Exception e) {
            mcb.append("错误：").append(e.toString());
        }

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onFriendMessageEvent(FriendMessageEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();

//        直接对话
        if (CurrentChattingUtil.getType().equals(CurrentChattingType.FRIEND)) {
            if (event.getSender().getId() == config.getOwnerQQ()) {

//                来自主人的指令不往外发
                if (event.getMessage().contentToString().startsWith(CommandManager.INSTANCE.getCommandPrefix())) {
                    return;
                }
                try {
                    BotUtil.getBotInstance().getFriend(CurrentChattingUtil.getQq()).sendMessage(event.getMessage());
                    return;
                } catch (Exception e) {
                    BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage("直接对话设置中的好友QQ不存在，发送失败");
                    return;
                }
            } else {
                if (event.getSender().getId() == CurrentChattingUtil.getQq()) {
                    BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(event.getMessage());
                    return;
                }
            }
        } else if (CurrentChattingUtil.getType().equals(CurrentChattingType.GROUP)) {
            if (event.getSender().getId() == config.getOwnerQQ()) {

//                不发指令
                if (event.getMessage().contentToString().startsWith(CommandManager.INSTANCE.getCommandPrefix())) {
                    return;
                }

                try {
                    BotUtil.getBotInstance().getGroup(CurrentChattingUtil.getGroupId()).sendMessage(event.getMessage());
                    return;
                } catch (Exception e) {
                    BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage("直接对话设置中的群不存在，发送失败");
                    return;
                }
            }
        } else if (CurrentChattingUtil.getType().equals(CurrentChattingType.TEMP)) {
            if (event.getSender().getId() == config.getOwnerQQ()) {

//                不发指令
                if (event.getMessage().contentToString().startsWith(CommandManager.INSTANCE.getCommandPrefix())) {
                    return;
                }

                Group group;
                try {
                    group = BotUtil.getBotInstance().getGroup(CurrentChattingUtil.getGroupId());
                } catch (Exception e) {
                    BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage("直接对话设置中的群不存在，发送失败");
                    return;
                }

                NormalMember normalMember;
                try {
                    normalMember = group.get(CurrentChattingUtil.getQq());
                } catch (Exception e) {
                    BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage("直接对话设置中指定群成员不存在，发送失败");
                    return;
                }

                normalMember.sendMessage(event.getMessage());
                return;
            } else {
                if (event.getSender().getId() == CurrentChattingUtil.getQq()) {
//                    todo: 此处经测试无法发送临时会话消息，原因不详，但按照理论是能够成功使用的
                    BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(event.getMessage());
                    return;
                }
            }
        }

//            如果所涉及的用户不是直接对话用户，则离开该分支条件并继续执行

        if (!config.getEvents().get("friendMessageEvent")) {
            return;
        }

//        来自主人的消息不必重申
        if (event.getSender().getId() == config.getOwnerQQ()) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("好友QQ：").append(String.valueOf(event.getSender().getId())).append("\n")
                .append("好友备注：").append(event.getSender().getRemark()).append("\n")
                .append("好友昵称：").append(event.getSender().getNick()).append("\n")
                .append("时间：").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date((long) event.getTime() * 1000L)))
                .append("\n");

        try {
            mcb.append("内容：\n").append(event.getMessage());
        } catch (Exception e) {
            mcb.append("错误：").append(e.toString());
        }

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onGroupTempMessageEvent(GroupTempMessageEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();

        if (CurrentChattingUtil.getType().equals(CurrentChattingType.TEMP)) {
            if (event.getGroup().getId() == CurrentChattingUtil.getGroupId() &&
            event.getSender().getId() == CurrentChattingUtil.getQq()) {
                BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(event.getMessage());
                return;
            }
        }

        if (!config.getEvents().get("groupTempMessageEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("发送者QQ：").append(String.valueOf(event.getSender().getId())).append("\n")
                .append("发送者群昵称：").append(event.getSender().getNameCard()).append("\n")
                .append("发送者昵称：").append(event.getSender().getNick()).append("\n")
                .append("来自群：").append(String.valueOf(event.getGroup().getId())).append("\n")
                .append("群名：").append(event.getGroup().getName()).append("\n")
                .append("时间：").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date((long) event.getTime() * 1000L)))
                .append("\n");

        try {
            mcb.append("内容：\n").append(event.getMessage());
        } catch (Exception e) {
            mcb.append("错误：").append(e.toString());
        }

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onStrangerMessageEvent(StrangerMessageEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("strangerMessageEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("发送者QQ：").append(String.valueOf(event.getSender().getId())).append("\n")
                .append("发送者昵称：").append(event.getSender().getNick()).append("\n")
                .append("时间：").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date((long) event.getTime() * 1000L)))
                .append("\n");

        try {
            mcb.append("内容：\n").append(event.getMessage());
        } catch (Exception e) {
            mcb.append("错误：").append(e.toString());
        }

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onOtherClientMessageEvent(OtherClientMessageEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("otherClientMessageEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("发送者：").append(event.getClient().toString()).append("\n")
                .append("时间：").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date((long) event.getTime() * 1000L)))
                .append("\n");

        try {
            mcb.append("内容：\n").append(event.getMessage()).build();
        } catch (Exception e) {
            mcb.append("错误：").append(e.toString());
        }

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onGroupMessagePostSendEvent(GroupMessagePostSendEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();

        if (CurrentChattingUtil.getType().equals(CurrentChattingType.GROUP)) {
            if (event.getTarget().getId() == CurrentChattingUtil.getGroupId()) {
                return;
            }
        }

        if (!config.getEvents().get("groupMessagePostSendEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("群号：").append(String.valueOf(event.getTarget().getId())).append("\n")
                .append("群名：").append(event.getTarget().getName()).append("\n");

        try {
            mcb.append("内容：\n").append(event.getMessage());
        } catch (Exception e) {
            mcb.append("错误：").append(e.toString());
        }

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onFriendMessagePostSendEvent(FriendMessagePostSendEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();

        //        防止刷屏，不向自己发送消息
        if (event.getTarget().getId() == config.getOwnerQQ()) {
            return;
        }

//        直接对话bot向外发送时不显示该事件
//        缺点是：对方发送的指令的结果无法返回给主人
        if (CurrentChattingUtil.getType().equals(CurrentChattingType.FRIEND) ||
                CurrentChattingUtil.getType().equals(CurrentChattingType.TEMP)) {
            if (event.getTarget().getId() == CurrentChattingUtil.getQq()) {
                return;
            }
        }

        if (!config.getEvents().get("friendMessagePostSendEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("好友QQ：").append(String.valueOf(event.getTarget().getId())).append("\n")
                .append("备注：").append(event.getTarget().getRemark()).append("\n")
                .append("昵称：").append(event.getTarget().getNick()).append("\n");

        try {
            mcb.append("内容：\n").append(event.getMessage()).build();
        } catch (Exception e) {
            mcb.append("错误：").append(e.toString());
        }

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onGroupTempMessagePostSendEvent(GroupTempMessagePostSendEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();

        if (CurrentChattingUtil.getType().equals(CurrentChattingType.TEMP)) {
            if (CurrentChattingUtil.getGroupId() == event.getGroup().getId() &&
            CurrentChattingUtil.getQq() == event.getTarget().getId()) {
                return;
            }
        }

        if (!config.getEvents().get("groupTempMessagePostSendEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("对方QQ：").append(String.valueOf(event.getTarget().getId())).append("\n")
                .append("对方群昵称：").append(event.getTarget().getNameCard()).append("\n")
                .append("对方昵称：").append(event.getTarget().getNick()).append("\n")
                .append("群号：").append(String.valueOf(event.getTarget().getGroup().getId())).append("\n")
                .append("群名：").append(event.getTarget().getGroup().getName()).append("\n");

        try {
            mcb.append("内容：\n").append(event.getMessage());
        } catch (Exception e) {
            mcb.append("错误：").append(e.toString());
        }

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onStrangerMessagePostSendEvent(StrangerMessagePostSendEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("strangerMessagePostSendEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("对方QQ：").append(String.valueOf(event.getTarget().getId())).append("\n")
                .append("对方昵称：").append(event.getTarget().getNick()).append("\n");

        try {
            mcb.append("内容：\n").append(event.getMessage());
        } catch (Exception e) {
            mcb.append("错误：").append(e.toString());
        }


        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onMessageRecallEvent(MessageRecallEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("messageRecallEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder();
        mcb.append("事件：").append(event.getClass().getSimpleName()).append("\n");

        UserOrBot author = event.getAuthor();
        if (event instanceof MessageRecallEvent.FriendRecall) {
            Friend friend = (Friend) author;
            mcb.append("撤回事件：").append(MessageRecallEvent.FriendRecall.class.getSimpleName()).append("\n")
                    .append("好友QQ：").append(String.valueOf(friend.getId())).append("\n")
                    .append("好友备注：").append(friend.getRemark()).append("\n")
                    .append("昵称：").append(friend.getNick()).append("\n");
        } else if (event instanceof MessageRecallEvent.GroupRecall) {
            NormalMember normalMember = (NormalMember) author;
            mcb.append("撤回事件：").append(MessageRecallEvent.GroupRecall.class.getSimpleName()).append("\n")
                    .append("发送者QQ：").append(String.valueOf(normalMember.getId())).append("\n")
                    .append("群昵称：").append(normalMember.getNameCard()).append("\n")
                    .append("昵称：").append(normalMember.getNick()).append("\n")
                    .append("群：").append(String.valueOf(normalMember.getGroup().getId())).append("\n")
                    .append("群名：").append(normalMember.getGroup().getName()).append("\n");
            if (normalMember.getId() != ((MessageRecallEvent.GroupRecall) event).getOperator().getId()) {
                mcb.append("执行者QQ：").append(String.valueOf(((MessageRecallEvent.GroupRecall) event).getOperator().getId())).append("\n")
                        .append("执行者群昵称：").append(((MessageRecallEvent.GroupRecall) event).getOperator().getNameCard()).append("\n")
                        .append("执行者昵称：").append(((MessageRecallEvent.GroupRecall) event).getOperator().getNick()).append("\n");
            }
        }

        mcb.append("消息id：").append(Arrays.toString(event.getMessageIds())).append("\n")
                .append("消息内部id：").append(Arrays.toString(event.getMessageInternalIds())).append("\n")
                .append("消息时间：").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date((long) event.getMessageTime() * 1000L)))
                .append("\n");

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());

        // TODO: How could I get Message instance through messageId?

    }

    @EventHandler
    public void onNudgeEvent(NudgeEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("nudgeEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder();
        mcb.append("事件：").append(event.getClass().getSimpleName()).append("\n");

        UserOrBot from = event.getFrom();
        UserOrBot target = event.getTarget();

        mcb.append("类型：").append(event.getSubject().getClass().getSimpleName()).append("\n");

        if (event.getSubject() instanceof Group) {

            try {
                NormalMember memberFrom = (NormalMember) from;

                mcb.append("来自：\n");
                mcb.append("    群：").append(String.valueOf(memberFrom.getGroup().getId())).append("\n")
                        .append("    群名：").append(memberFrom.getGroup().getName()).append("\n")
                        .append("    成员QQ：").append(String.valueOf(memberFrom.getId())).append("\n")
                        .append("    成员群昵称：").append(memberFrom.getNameCard()).append("\n")
                        .append("    成员昵称：").append(memberFrom.getNick()).append("\n");
            } catch (ClassCastException ignored) {

            }

            try {
                NormalMember memberTarget = (NormalMember) target;

                mcb.append("目标：\n");
                mcb.append("    成员QQ：").append(String.valueOf(memberTarget.getId())).append("\n")
                        .append("    成员群昵称：").append(memberTarget.getNameCard()).append("\n")
                        .append("    成员昵称：").append(memberTarget.getNick()).append("\n");
            } catch (ClassCastException ignored) {

            }
        } else if (event.getSubject() instanceof Friend) {

            try {
                Friend friendFrom = (Friend) from;

                mcb.append("来自：\n");
                mcb.append("    好友QQ：").append(String.valueOf(friendFrom.getId())).append("\n")
                        .append("    好友备注：").append(friendFrom.getRemark()).append("\n")
                        .append("    好友昵称：").append(friendFrom.getNick()).append("\n");
            } catch (ClassCastException ignored) {

            }

        }

        mcb.append("行为：").append(event.getAction()).append("\n")
                .append("后缀：").append(event.getSuffix());

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onGroupNameChangeEvent(GroupNameChangeEvent event) throws IOException {
//        mirai console 2.7测试无效

        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("groupNameChangeEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("群：").append(String.valueOf(event.getGroupId())).append("\n")
                .append("原群名：").append(event.getOrigin()).append("\n")
                .append("现群名：").append(event.getNew()).append("\n")
                .append("执行者QQ：").append(String.valueOf(event.getOperator().getId())).append("\n")
                .append("群昵称：").append(event.getOperator().getNameCard()).append("\n")
                .append("昵称：").append(event.getOperator().getNick()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onGroupEntranceAnnouncementChangeEvent(GroupEntranceAnnouncementChangeEvent event) throws IOException {
//        不理解这个是什么

        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("groupEntranceAnnouncementChangeEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("原公告：").append(event.getOrigin()).append("\n")
                .append("现公告：").append(event.getNew()).append("\n")
                .append("执行者QQ：").append(String.valueOf(event.getOperator().getId())).append("\n")
                .append("群昵称：").append(event.getOperator().getNameCard()).append("\n")
                .append("昵称：").append(event.getOperator().getNick()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onGroupMuteAllEvent(GroupMuteAllEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("groupMuteAllEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("原状态：").append(String.valueOf(event.getOrigin())).append("\n")
                .append("现状态：").append(String.valueOf(event.getNew())).append("\n")
                .append("执行者QQ：").append(String.valueOf(event.getOperator().getId())).append("\n")
                .append("群昵称：").append(event.getOperator().getNameCard()).append("\n")
                .append("昵称：").append(event.getOperator().getNick()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onGroupAllowAnonymousChatEvent(GroupAllowAnonymousChatEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("groupAllowAnonymousChatEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("原状态：").append(String.valueOf(event.getOrigin())).append("\n")
                .append("现状态：").append(String.valueOf(event.getNew())).append("\n")
                .append("执行者QQ：").append(String.valueOf(event.getOperator().getId())).append("\n")
                .append("群昵称：").append(event.getOperator().getNameCard()).append("\n")
                .append("昵称：").append(event.getOperator().getNick()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onGroupAllowMemberInviteEvent(GroupAllowMemberInviteEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("groupAllowMemberInviteEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("原状态：").append(String.valueOf(event.getOrigin())).append("\n")
                .append("现状态：").append(String.valueOf(event.getNew())).append("\n")
                .append("执行者QQ：").append(String.valueOf(event.getOperator().getId())).append("\n")
                .append("群昵称：").append(event.getOperator().getNameCard()).append("\n")
                .append("昵称：").append(event.getOperator().getNick()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onBotLeaveEvent(BotLeaveEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("botLeaveEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("事件字符串：").append(event.toString()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onBotMuteEvent(BotMuteEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("botMuteEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("禁言时间（秒）：").append(String.valueOf(event.getDurationSeconds())).append("\n")
                .append("群：").append(String.valueOf(event.getGroup().getId())).append("\n")
                .append("群名：").append(event.getGroup().getName()).append("\n")
                .append("执行者QQ：").append(String.valueOf(event.getOperator().getId())).append("\n")
                .append("群昵称：").append(event.getOperator().getNameCard()).append("\n")
                .append("昵称：").append(event.getOperator().getNick()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onBotUnmuteEvent(BotUnmuteEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("botUnmuteEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("群：").append(String.valueOf(event.getGroup().getId())).append("\n")
                .append("群名：").append(event.getGroup().getName()).append("\n")
                .append("执行者QQ：").append(String.valueOf(event.getOperator().getId())).append("\n")
                .append("群昵称：").append(event.getOperator().getNameCard()).append("\n")
                .append("昵称：").append(event.getOperator().getNick()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onBotJoinGroupEvent(BotJoinGroupEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("botJoinGroupEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("群：").append(String.valueOf(event.getGroup().getId())).append("\n")
                .append("群名：").append(event.getGroup().getName()).append("\n")
                .append("事件字符串：").append(event.toString()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onMemberJoinEvent(MemberJoinEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("memberJoinEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("群：").append(String.valueOf(event.getGroup().getId())).append("\n")
                .append("群名：").append(event.getGroup().getName()).append("\n")
                .append("新成员QQ：").append(String.valueOf(event.getMember().getId())).append("\n")
                .append("昵称：").append(event.getMember().getNick()).append("\n")
                .append("事件字符串：").append(event.toString()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onMemberLeaveEvent(MemberLeaveEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("memberLeaveEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("群：").append(String.valueOf(event.getGroup().getId())).append("\n")
                .append("群名：").append(event.getGroup().getName()).append("\n")
                .append("离开成员QQ：").append(String.valueOf(event.getMember().getId())).append("\n")
                .append("群昵称：").append(event.getMember().getNameCard()).append("\n")
                .append("昵称：").append(event.getMember().getNick()).append("\n")
                .append("事件字符串：").append(event.toString()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onMemberJoinRequestEvent(MemberJoinRequestEvent event) throws IOException {
        MemberJoinRequestEventUtil.insert(event);

        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("memberJoinRequestEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("事件号：").append(String.valueOf(event.getEventId())).append("\n")
                .append("群：").append(String.valueOf(event.getGroupId())).append("\n")
                .append("群名：").append(event.getGroupName()).append("\n")
                .append("申请人QQ：").append(String.valueOf(event.getFromId())).append("\n")
                .append("昵称：").append(event.getFromNick()).append("\n")
                .append("申请原因：").append(event.getMessage()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onBotInvitedJoinGroupRequestEvent(BotInvitedJoinGroupRequestEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("botInvitedJoinGroupRequestEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("事件号：").append(String.valueOf(event.getEventId())).append("\n")
                .append("群：").append(String.valueOf(event.getGroupId())).append("\n")
                .append("群名：").append(event.getGroupName()).append("\n")
                .append("邀请人QQ：").append(String.valueOf(event.getInvitorId())).append("\n")
                .append("昵称：").append(event.getInvitorNick()).append("\n").build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);

        BotInvitedJoinGroupRequestEventUtil.insert(event);
    }

    @EventHandler
    public void onMemberCardChangeEvent(MemberCardChangeEvent event) throws IOException {
//        似乎只有在bot主动修改时才有效

        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("memberCardChangeEvent")) {
            return;
        }

        MessageChain mc = new MessageChainBuilder()
                .append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("群：").append(String.valueOf(event.getGroupId())).append("\n")
                .append("群名：").append(event.getGroup().getName()).append("\n")
                .append("成员QQ：").append(String.valueOf(event.getMember().getId())).append("\n")
                .append("昵称：").append(event.getMember().getNick()).append("\n")
                .append("原群昵称：").append(event.getOrigin()).append("\n")
                .append("现群昵称：").append(event.getNew()).build();

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mc);
    }

    @EventHandler
    public void onMemberSpecialTitleChangeEvent(MemberSpecialTitleChangeEvent event) throws IOException {
//        测试中发现并未触发该事件

        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("memberSpecialTitleChangeEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder();
        mcb.append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("群：").append(String.valueOf(event.getGroupId())).append("\n")
                .append("群名：").append(event.getGroup().getName()).append("\n")
                .append("成员QQ：").append(String.valueOf(event.getMember().getId())).append("\n")
                .append("群昵称：").append(event.getMember().getNameCard()).append("\n")
                .append("昵称：").append(event.getMember().getNick()).append("\n")
                .append("原头衔：").append(event.getOrigin()).append("\n")
                .append("现头衔：").append(event.getNew());

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onMemberPermissionChangeEvent(MemberPermissionChangeEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("memberPermissionChangeEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder();
        mcb.append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("群：").append(String.valueOf(event.getGroupId())).append("\n")
                .append("群名：").append(event.getGroup().getName()).append("\n")
                .append("成员QQ：").append(String.valueOf(event.getMember().getId())).append("\n")
                .append("群昵称：").append(event.getMember().getNameCard()).append("\n")
                .append("昵称：").append(event.getMember().getNick()).append("\n")
                .append("原权限：").append(event.getOrigin().toString()).append("\n")
                .append("现权限：").append(event.getNew().toString());

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onMemberMuteEvent(MemberMuteEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("memberMuteEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder();
        mcb.append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("群：").append(String.valueOf(event.getGroupId())).append("\n")
                .append("群名：").append(event.getGroup().getName()).append("\n")
                .append("成员QQ：").append(String.valueOf(event.getMember().getId())).append("\n")
                .append("群昵称：").append(event.getMember().getNameCard()).append("\n")
                .append("昵称：").append(event.getMember().getNick()).append("\n")
                .append("执行者QQ：").append(String.valueOf(event.getOperator().getId())).append("\n")
                .append("群昵称：").append(event.getOperator().getNameCard()).append("\n")
                .append("昵称：").append(event.getOperator().getNick()).append("\n")
                .append("禁言时长（秒）：").append(String.valueOf(event.getDurationSeconds()));

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onMemberUnmuteEvent(MemberUnmuteEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("memberUnmuteEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder();
        mcb.append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("群：").append(String.valueOf(event.getGroupId())).append("\n")
                .append("群名：").append(event.getGroup().getName()).append("\n")
                .append("成员QQ：").append(String.valueOf(event.getMember().getId())).append("\n")
                .append("群昵称：").append(event.getMember().getNameCard()).append("\n")
                .append("昵称：").append(event.getMember().getNick()).append("\n")
                .append("执行者QQ：").append(String.valueOf(event.getOperator().getId())).append("\n")
                .append("群昵称：").append(event.getOperator().getNameCard()).append("\n")
                .append("昵称：").append(event.getOperator().getNick());

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onFriendAddEvent(FriendAddEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("friendAddEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder();
        mcb.append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("新好友QQ：").append(String.valueOf(event.getFriend().getId())).append("\n")
                .append("备注：").append(event.getFriend().getRemark()).append("\n")
                .append("昵称：").append(event.getFriend().getNick());

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onFriendDeleteEvent(FriendDeleteEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("friendDeleteEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder();
        mcb.append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("好友QQ：").append(String.valueOf(event.getFriend().getId())).append("\n")
                .append("备注：").append(event.getFriend().getRemark()).append("\n")
                .append("昵称：").append(event.getFriend().getNick());

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onNewFriendRequestEvent(NewFriendRequestEvent event) throws IOException {
        NewFriendRequestEventUtil.insert(event);

        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("newFriendRequestEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder();
        mcb.append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("事件号：").append(String.valueOf(event.getEventId())).append("\n")
                .append("QQ：").append(String.valueOf(event.getFromId())).append("\n")
                .append("昵称：").append(event.getFromNick()).append("\n")
                .append("来自群：").append(String.valueOf(event.getFromGroupId())).append("\n");

        try {
            mcb.append("群名：").append(event.getFromGroup().getName()).append("\n");
        } catch (Exception e) {
            mcb.append("错误：").append(e.toString()).append("\n");
        }
        mcb.append("内容：").append(event.getMessage());

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onFriendAvatarChangedEvent(FriendAvatarChangedEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("friendAvatarChangedEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder();
        mcb.append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("好友QQ：").append(String.valueOf(event.getFriend().getId())).append("\n")
                .append("备注：").append(event.getFriend().getRemark()).append("\n")
                .append("昵称：").append(event.getFriend().getNick()).append("\n")
                .append("事件字符串：").append(event.toString());

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onFriendNickChangedEvent(FriendNickChangedEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("friendNickChangedEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder();
        mcb.append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("好友QQ：").append(String.valueOf(event.getFriend().getId())).append("\n")
                .append("备注：").append(event.getFriend().getRemark()).append("\n")
                .append("原昵称：").append(event.getFrom()).append("\n")
                .append("现昵称：").append(event.getTo());

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

    @EventHandler
    public void onFriendInputStatusChangedEvent(FriendInputStatusChangedEvent event) throws IOException {
        Config config = ConfigUtil.getConfig();
        if (!config.getEvents().get("friendInputStatusChangedEvent")) {
            return;
        }

        MessageChainBuilder mcb = new MessageChainBuilder();
        mcb.append("事件：").append(event.getClass().getSimpleName()).append("\n")
                .append("好友QQ：").append(String.valueOf(event.getFriend().getId())).append("\n")
                .append("备注：").append(event.getFriend().getRemark()).append("\n")
                .append("昵称：").append(event.getFriend().getNick()).append("\n")
                .append("状态：").append(String.valueOf(event.getInputting()));

        BotUtil.getBotInstance().getFriend(config.getOwnerQQ()).sendMessage(mcb.build());
    }

}
